package com.example.xhhweather.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.xhhweather.FragmentListener;
import com.example.xhhweather.HttpUtil;
import com.example.xhhweather.MainActivity;
import com.example.xhhweather.R;
import com.example.xhhweather.SelectCity;
import com.example.xhhweather.Utility;
import com.example.xhhweather.gson.Weather;
import com.example.xhhweather.gson.WeatherResults;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static android.content.Context.MODE_PRIVATE;

public class CityFragment extends Fragment{
    private View view;
    private Button btnSelectCity;
    private TextView weatherTitleCityname;
    private TextView weatherTitleCitypinyin;

    private TextView tvFirstDayDate;
    private TextView tvFirstDayPhe;
    private TextView tvFirstDayHigh;
    private TextView tvFirstDayLow;

    private TextView tvSecondDayDate;
    private TextView tvSecondDayPhe;
    private TextView tvSecondDayHigh;
    private TextView tvSecondDayLow;

    private TextView tvThirdDayDate;
    private TextView tvThirdDayPhe;
    private TextView tvThirdDayHigh;
    private TextView tvThirdDayLow;

    private TextView temperatureText;
    private TextView caseText;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.city_fragment,container,false);
        initfindViewById();
        //GetrequestWeather("guangzhou");

        btnSelectCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), SelectCity.class);
                startActivity(intent);
            }
        });

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(getContext(),"create",Toast.LENGTH_SHORT).show();

    }

    /**
     * 初始化
     */
    private void initfindViewById(){
        btnSelectCity=view.findViewById(R.id.manage_city_btn);
        weatherTitleCityname=view.findViewById(R.id.weather_title_cityname);
        weatherTitleCitypinyin=view.findViewById(R.id.weather_title_citypinyin);


        temperatureText=view.findViewById(R.id.temperature_text);
        caseText=view.findViewById(R.id.case_text);

        tvFirstDayDate=view.findViewById(R.id.tv_firstday_date);
        tvFirstDayPhe=view.findViewById(R.id.tv_firstday_phe);
        tvFirstDayHigh=view.findViewById(R.id.tv_firstday_high);
        tvFirstDayLow=view.findViewById(R.id.tv_firstday_low);

        tvSecondDayDate=view.findViewById(R.id.tv_secondday_date);
        tvSecondDayPhe=view.findViewById(R.id.tv_secondday_phe);
        tvSecondDayHigh=view.findViewById(R.id.tv_secondday_high);
        tvSecondDayLow=view.findViewById(R.id.tv_secondday_low);

        tvThirdDayDate=view.findViewById(R.id.tv_thirdday_date);
        tvThirdDayPhe=view.findViewById(R.id.tv_thirdday_phe);
        tvThirdDayHigh=view.findViewById(R.id.tv_thirdday_high);
        tvThirdDayLow=view.findViewById(R.id.tv_thirdday_low);


        //refreshLayout=view.findViewById(R.id.swipe_refresh);
    }
    /**
     * 获得请求后的天气数据，后显示showWeatherInfo（）
     * @param cityPinYin
     */
    public void GetrequestWeather(String cityPinYin){
        String url="https://api.seniverse.com/v3/weather/daily.json?key=SojsF4VNkV_cTQexq&location=" + cityPinYin + "&language=zh-Hans&unit=c&start=0&days=5";


        HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String weatherData=response.body().string();
                Log.e("MainActivity","得到的数据"+weatherData);
                final Weather weatherInfo= Utility.handleWeatherRsponse(weatherData);
                //showWeatherInfo(weatherInfo);
            }
        });
    }

    /**
     * 显示天气信息
     * @param weatherInfo
     */
    private void showWeatherInfo(final Weather weatherInfo){
       getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                WeatherResults results=weatherInfo.getResults().get(0);
                String cityName=results.getLocation().getName();//城市名称
                weatherTitleCityname.setText(cityName);

                //将3天内的天气数据获得后存入二维数组
                String [] [] resultDaily=new String[3][5];
                for(int i=0;i<3;i++){
                    for(int j=0;j<4;){
                        resultDaily[i][j]=results.getDaily().get(i).getDate();j++;
                        resultDaily[i][j]=results.getDaily().get(i).getText_day();j++;
                        resultDaily[i][j]=results.getDaily().get(i).getHigh();j++;
                        resultDaily[i][j]=results.getDaily().get(i).getLow();j++;
                    }
                }
                temperatureText.setText(resultDaily[0][2]+"℃");
                caseText.setText(resultDaily[0][1]);

                for(int j=0;j<4;){
                    //Log.e("resultDaily",resultDaily[0][j]);
                    tvFirstDayDate.setText(resultDaily[0][j]);j++;
                    tvFirstDayPhe.setText(resultDaily[0][j]);j++;
                    tvFirstDayHigh.setText(resultDaily[0][j]);j++;
                    tvFirstDayLow.setText(resultDaily[0][j]);j++;
                }
                for(int j=0;j<4;){
                    tvSecondDayDate.setText(resultDaily[1][j]);j++;
                    tvSecondDayPhe.setText(resultDaily[1][j]);j++;
                    tvSecondDayHigh.setText(resultDaily[1][j]);j++;
                    tvSecondDayLow.setText(resultDaily[1][j]);j++;
                }
                for(int j=0;j<4;){
                    tvThirdDayDate.setText(resultDaily[2][j]);j++;
                    tvThirdDayPhe.setText(resultDaily[2][j]);j++;
                    tvThirdDayHigh.setText(resultDaily[2][j]);j++;
                    tvThirdDayLow.setText(resultDaily[2][j]);j++;
                }
            }
        });
    }

}
